<?php 
 header("Content-type:text/html;charset=utf-8");
include_once('system/dbConn.php');
connect();
//使用$_POST数组获取表单中输入的数据
 $username = trim($_POST['username']);
  $password = $_POST['password'];
  $gender = $_POST['gender'];
  $birthdate=$_POST['birthdate'];
  $hobby = implode($_POST['hobby'],',');
  //将数组转换成字符串
  $degree = $_POST['degree'];
  $intro = $_POST['intro'];
  //var_dump($_FILES);

//上传文件大小的确定
if($_FILES["pic"]["error"]>0)
{
  switch($_FILES["pic"]["error"]){
  	case 1:echo"文件尺寸超过了配置文件的最大值"; exit;
  	case 3:echo"部分文件上传";exit;
  	case 4:echo"没有选择头像文件！";
  	$sql="insert into users values(null,'$username','$password','$gender','$birthdate','$hobby','$degree','$intro',null)";
  	break;
  	default:echo"未知错误";exit;
  }
}else{
	$suffix=strrchr($_FILES["pic"]["name"],'.');
    //echo $suffix;
	//判断文件类型是否图片
$allowtype=array("jpg","jpeg","png","gif","Bmp","flv");
if(!in_array(ltrim($suffix,'.'), $allowtype))
{
  
	echo"文件类型为$suffix!<br/>";
	echo"文件类型不正确！只能选择扩展名为jpg,jpeg,png,gif,Bmp,flv类型的文件！";
	exit;
}
$filepath="images/";
$newname=date("YmdHis").rand(100,999).$suffix;
//echo $filepath.$newname.'<br/>';
if(!move_uploaded_file($_FILES["pic"]["tmp_name"], 
	$filepath.$newname)){
	die('图片上传失败');
}
else{
	//编写sql语句
	$sql="insert into users values(null,'$username','$password','$gender','$birthdate','$hobby','$degree','$intro','$newname')";
}
}
//echo $sql.'<br/>';
//执行SQL语句
$result=mysql_query($sql) or die('插入失败！'.mysql_error());
//判断是否插入成功
$num=mysql_affected_rows();
if($num!=1){
  echo"注册失败!<br/>";
  echo"返回<a href='./admin/login.html'>返回</a>";
}else{
	echo"注册成功！";
	echo"请使用<a href='./admin/login.html'>管理员登录</a>查看用户信息，";
	echo"或返回<a href='userReg.html'>注册</a>页面！";
}
 ?>

